﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface IModuleService : IBaseService<Module> {
        Module GetNext();
    }

    public class ModuleService : BaseService, IModuleService {
        public IList<Module> GetList(Func<Module, bool> filters) {
            if (filters != null)
                return db.Modules.Where(filters).ToList();
            else
                return db.Modules.ToList();
        }

        public Module Find(params object[] keys) {
            return db.Modules.Find(keys);
        }

        public void Add(Module model) {
            db.Modules.Add(model);
        }

        public void Update(Module model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Module model) {
            db.Modules.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public Module GetNext() {
            int id = 1;
            if (db.Modules.Count() > 0)
                id = db.Modules.Max(c => c.Id) + 1;
            return new Module { Id = id };
        }
    }
}